package com.ruoyi.finance.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.finance.domain.SubjectSubsidiary;
import com.ruoyi.finance.domain.TSubsidiary;
import com.ruoyi.finance.domain.vo.TSubjectVo;
import com.ruoyi.finance.mapper.SubjectSubsidiaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.TSubjectMapper;
import com.ruoyi.finance.domain.TSubject;
import com.ruoyi.finance.service.ITSubjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 科目表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-25
 */
@Service
public class TSubjectServiceImpl implements ITSubjectService 
{
    @Autowired
    private TSubjectMapper tSubjectMapper;

    @Autowired
    SubjectSubsidiaryMapper subjectSubsidiaryMapper;

    /**
     * 查询科目表
     * 
     * @param id 科目表主键
     * @return 科目表
     */
    @Override
    public TSubject selectTSubjectById(Long id)
    {
        return tSubjectMapper.selectTSubjectById(id);
    }

    /**
     * 查询科目表列表
     * 
     * @param tSubject 科目表
     * @return 科目表
     */
    @Override
    public List<TSubject> selectTSubjectList(TSubject tSubject)
    {
        return tSubjectMapper.selectTSubjectList(tSubject);
    }

    /**
     * 新增科目表
     * 
     * @param tSubject 科目表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTSubject(TSubject tSubject)
    {
        List<TSubject> tSubject1 = tSubjectMapper.selectList(new QueryWrapper<TSubject>().lambda().eq(TSubject::getNumber, tSubject.getNumber()));
        if (tSubject1.size() > 0) {
            throw new RuntimeException("科目编码已存在");
        }
        List<TSubject> tSubject2 = tSubjectMapper.selectList(new QueryWrapper<TSubject>().lambda().eq(TSubject::getName, tSubject.getName()));
        if (tSubject2.size() > 0) {
            throw new RuntimeException("科目名称已存在");
        }
        int[] subsidiaryIds = tSubject.getSubsidiaryIds();
        if (subsidiaryIds != null && subsidiaryIds.length > 0) {
            for (int i = 0; i < subsidiaryIds.length; i++) {
                SubjectSubsidiary subjectSubsidiary = new SubjectSubsidiary();
                subjectSubsidiary.setSubjectId(Integer.parseInt(tSubject.getNumber()));
                subjectSubsidiary.setSubsidiaryId(subsidiaryIds[i]);
                subjectSubsidiaryMapper.insert(subjectSubsidiary);
            }
        }
        return tSubjectMapper.insert(tSubject);

    }

    /**
     * 修改科目表
     * 
     * @param tSubject 科目表
     * @return 结果
     */
    @Override
    public int updateTSubject(TSubject tSubject)
    {
        List<TSubject> tSubject1 = tSubjectMapper.selectList(new QueryWrapper<TSubject>().lambda().eq(TSubject::getNumber, tSubject.getNumber()));
        if (tSubject1.size() > 1) {
            throw new RuntimeException("科目编码已存在");
        }
        List<TSubject> tSubject2 = tSubjectMapper.selectList(new QueryWrapper<TSubject>().lambda().eq(TSubject::getName, tSubject.getName()));
        if (tSubject2.size() > 1) {
            throw new RuntimeException("科目名称已存在");
        }
        int[] subsidiaryIds = tSubject.getSubsidiaryIds();
        if (subsidiaryIds != null && subsidiaryIds.length > 0) {
            for (int i = 0; i < subsidiaryIds.length; i++) {
                subjectSubsidiaryMapper.delete(new QueryWrapper<SubjectSubsidiary>().lambda().eq(SubjectSubsidiary::getSubjectId, tSubject.getNumber()));
                SubjectSubsidiary subjectSubsidiary = new SubjectSubsidiary();
                subjectSubsidiary.setSubjectId(Integer.parseInt(tSubject.getNumber()));
                subjectSubsidiary.setSubsidiaryId(subsidiaryIds[i]);
                subjectSubsidiaryMapper.insert(subjectSubsidiary);
            }
        }
        return tSubjectMapper.updateById(tSubject);
    }

    /**
     * 批量删除科目表
     * 
     * @param ids 需要删除的科目表主键
     * @return 结果
     */
    @Override
    public int deleteTSubjectByIds(Long[] ids)
    {
        return tSubjectMapper.deleteTSubjectByIds(ids);
    }

    /**
     * 删除科目表信息
     * 
     * @param id 科目表主键
     * @return 结果
     */
    @Override
    public int deleteTSubjectById(Long id)
    {
        return tSubjectMapper.deleteTSubjectById(id);
    }

    @Override
    public List<TSubjectVo> getSubject(Long sid) {
//        List<TSubjectVo> list = tSubjectMapper.getSubject(sid);
        return null;
    }


}
